<template>
  <div class="file-tree">
    <div class="file-tool">
      <div class="file-label"> <i class="el-icon-files"></i>&nbsp;&nbsp;文档大纲 </div>
    </div>
    <div class="tree-main">
      <vxe-table
        resizable
        show-overflow
        :show-header="false"
        ref="xTree"
        size="small"
        height="auto"
        :column-config="{
          resizable: true
        }"
        :row-config="{
          keyField: 'path',
          useKey: true,
          height: 25,
          isHover: true
        }"
        :tree-config="{
          accordion: false,
          line: true,
          iconOpen: 'vxe-icon-square-minus',
          iconClose: 'vxe-icon-square-plus',
          expandAll: false,
          rowField: 'path',
          children: 'nodes',
          reserve: true
        }"
        :data="nodes"
        :scroll-y="{ enabled: false }"
      >
        <vxe-column field="name" title="Name" tree-node>
          <template #default="{ row }">
            <div
              class="file-node"
              draggable="true"
              :class="{
                active: selectNode && selectNode.id === row.id
              }"
              @click="openNode(row)"
            >
              <template v-if="row.type === 'directory'">
                <i
                  class="tree-node-icon"
                  :class="
                    $refs.xTree.isTreeExpandByRow(row) ? 'vxe-icon-folder-open' : 'vxe-icon-folder'
                  "
                ></i>
              </template>
              <template v-else>
                <i class="tree-node-icon vxe-icon-file-txt"></i>
              </template>
              <span class="file-name">{{ row.name }}</span>
            </div>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    nodes: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      selectNode: null // 当前选择的节点
    }
  },
  methods: {
    openNode(row) {
      this.selectNode = row
      this.$emit('openNode', row)
    }
  }
}
</script>

<style lang="less" scoped>
.file-tree {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;

  .file-tool {
    background-color: #73b8fa;
    flex: 0 0 25px;
    color: #fff;
    font-size: 14px;
    padding: 0px 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .btn {
      i {
        cursor: pointer;
        margin-left: 8px;
        color: #fff;

        &.active {
          background: #4181e2;
          border: 1px solid;
          border-radius: 2px;
          font-size: 12px;
          padding: 1px 2px;
        }
      }
    }
  }

  .tree-main {
    flex: 1;
    font-size: 14px;
    overflow: hidden;
  }

  .tree-node-icon {
    width: 16px;
    text-align: center;
  }

  :deep(.vxe-icon-square-minus, .vxe-icon-square-plus) {
    background-color: #ffffff;
  }

  :deep(.file-node) {
    position: relative;
    cursor: pointer;

    &:hover {
      .edit-btn {
        display: inline-block;
      }
    }

    .file-name {
      padding: 2px;
    }

    &.active {
      .file-name {
        background: #73b8fa;
        color: #ffffff;
      }
    }

    .edit-btn {
      display: none;
      padding: 3px;
      background: #fff;
      border-radius: 2px;
      position: absolute;
      top: 0;
      right: 0;
      font-size: 14px;
      color: #dd3333;

      i {
        margin-right: 5px;
        &:last-child {
          margin-right: 0;
        }
      }

      i:hover {
        color: red;
      }
    }
  }
}
</style>
